home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / mui / mui-tools / multiuser / src / support / setowner.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  4KB  |  152 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Set the owner of a file - AmigaOS 3.0 (V39+) version        *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <dos/dos.h>
  14. #include <dos/dosasl.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include <string.h>
  18. #include <libraries/multiuser.h>
  19. #include <proto/multiuser.h>
  20.  
  21. #include "SetOwner_rev.h"
  22.  
  23. #include "Locale.h"
  24.  
  25. char __VersTag__[] = VERSTAG;
  26.  
  27.  
  28. int __saveds Start(char *arg)
  29. {
  30.     struct ExecBase *SysBase;
  31.     struct DosLibrary *DOSBase;
  32.     struct muBase *muBase = NULL;
  33.     struct RDArgs *args;
  34.     LONG argarray[] = {
  35. #define argFILE    0
  36. #define argUSER    1
  37. #define argGROUP    2
  38. #define argNOBODY    3
  39. #define argALL        4
  40. #define argQUIET    5
  41.         NULL, NULL, NULL, NULL, NULL, NULL
  42.     };
  43.     ULONG user = NULL;
  44.     struct muUserInfo *uinfo;
  45.     struct muGroupInfo *ginfo;
  46.     struct AnchorPath *anchor;
  47.     int rc = RETURN_OK;
  48.     LONG error = NULL;
  49.     BPTR dir;
  50.     struct LocaleInfo li;
  51.  
  52.     SysBase = *(struct ExecBase **)4;
  53.     
  54.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 39))) ||
  55.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  56.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  57.         goto Exit;
  58.     }
  59.  
  60.     OpenLoc(&li);
  61.  
  62.     args = ReadArgs("FILE/A,USER,GROUP,NOBODY/S,ALL/S,QUIET/S", argarray, NULL);
  63.     if (!args)
  64.         error = IoErr();
  65.     else if (argarray[argUSER] && argarray[argNOBODY]) {
  66.         PutStr(GetLocS(&li,MSG_INVALID_OPTIONS));
  67.         rc = RETURN_ERROR;
  68.     } else {
  69.         if (argarray[argUSER]) {
  70.             if (uinfo = muAllocUserInfo()) {
  71.                 strncpy(uinfo->UserID, (char *)argarray[argUSER], muUSERIDSIZE-1);
  72.                 uinfo->UserID[muUSERIDSIZE-1] = '\0';
  73.                 if (muGetUserInfo(uinfo, muKeyType_UserID))
  74.                     user = (uinfo->uid<<16)|uinfo->gid;
  75.                 muFreeUserInfo(uinfo);
  76.             } else {
  77.                 PrintFault(IoErr(), NULL);
  78.                 goto Exit;
  79.             }
  80.             if (!user) {
  81.                 VPrintf(GetLocS(&li,MSG_UNKNOWN_USER), &argarray[argUSER]);
  82.                 rc = RETURN_ERROR;
  83.             }
  84.         } else if (!argarray[argNOBODY])
  85.             user = muGetTaskOwner(NULL);
  86.         if (argarray[argGROUP]) {
  87.             if (ginfo = muAllocGroupInfo()) {
  88.                 strncpy(ginfo->GroupID, (char *)argarray[argGROUP],
  89.                           muGROUPIDSIZE-1);
  90.                 ginfo->GroupID[muGROUPIDSIZE-1] = '\0';
  91.                 if (muGetGroupInfo(ginfo, muKeyType_GroupID))
  92.                     user = user & muMASK_UID | ginfo->gid;
  93.                 else {
  94.                     VPrintf(GetLocS(&li,MSG_UNKNOWN_GROUP), &argarray[argGROUP]);
  95.                     rc = RETURN_ERROR;
  96.                 }
  97.                 muFreeGroupInfo(ginfo);                
  98.             } else {
  99.                 PrintFault(IoErr(), NULL);
  100.                 goto Exit;
  101.             }
  102.         }
  103.         if (!rc)
  104.             if (anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath)+1024,
  105.                                                                      MEMF_CLEAR)) {
  106.                 anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  107.                 anchor->ap_Flags = APF_DOWILD;
  108.                 anchor->ap_Strlen = 1024;
  109.                 if (!(error = MatchFirst((char *)argarray[argFILE], anchor))) {
  110.                     do
  111.                         if (anchor->ap_Flags & APF_DIDDIR)
  112.                             anchor->ap_Flags &= ~APF_DIDDIR;
  113.                         else {
  114.                             if (argarray[argALL] && (anchor->ap_Info.fib_DirEntryType > 0))
  115.                                 anchor->ap_Flags |= APF_DODIR;
  116.                             dir = CurrentDir(DupLock(anchor->ap_Last->an_Lock));
  117.                             if (!SetOwner(anchor->ap_Info.fib_FileName, user)) {
  118.                                 PutStr(anchor->ap_Buf);
  119.                                 PrintFault(IoErr(), " ");
  120.                             } else if (!argarray[argQUIET]) {
  121.                                 PutStr(anchor->ap_Buf);
  122.                                 if (anchor->ap_Info.fib_DirEntryType > 0)
  123.                                     PutStr("(Dir)");
  124.                                 PutStr(GetLocS(&li,MSG_DONE));
  125.                             }
  126.                             UnLock(CurrentDir(dir));
  127.                         }
  128.                     while (!(error = MatchNext(anchor)));
  129.                     if (error == ERROR_NO_MORE_ENTRIES)
  130.                         error = NULL;
  131.                 } else if (error == ERROR_NO_MORE_ENTRIES)
  132.                     error = ERROR_OBJECT_NOT_FOUND;
  133.                 MatchEnd(anchor);
  134.                 FreeVec(anchor);
  135.             } else
  136.                 error = IoErr();
  137.     }
  138.     FreeArgs(args);
  139.     if (error) {
  140.         PrintFault(error, NULL);
  141.         rc = RETURN_ERROR;
  142.     }
  143.  
  144.     CloseLoc(&li);
  145.  
  146. Exit:
  147.     CloseLibrary((struct Library *)muBase);
  148.     CloseLibrary((struct Library *)DOSBase);
  149.  
  150.     return(rc);
  151. }
  152.